import {
  Toast,
  Dialog
} from 'mand-mobile'

export default Vue => {
  Vue.prototype.$toast = function (message, position = 'center', duration = 3000) {
    // Toast.info(message)
    Toast({
      content: message,
      position: position,
      duration: duration
    })
  }
  const DialogConstructor = Vue.extend(Dialog)
  const generate = function ({
    title = '',
    icon = '',
    content = '',
    closable = false,
    btns = []
  }) {
    const vm = new DialogConstructor({
      propsData: {
        value: true,
        title,
        icon,
        content,
        closable,
        btns
      }
    }).$mount()

    vm.$on('input', val => {
      if (!val) {
        vm.value = false
      }
    })
    vm.$on('hide', () => {
      vm.$destroy()
    })

    return vm
  }
  Vue.prototype.$confirm = ({
    title = '',
    icon = '',
    content = '',
    cancelText = '取消',
    confirmText = '确定'
  }) => {
    // eslint-disable-next-line no-undef
    return new Promise((resolve, reject) => {
      const vm = generate({
        title,
        icon,
        content,
        btns: [{
          text: cancelText,
          handler: () => {
            vm.close()
            reject(new Error())
          }
        },
        {
          text: confirmText,
          handler: () => {
            vm.close()
            resolve()
          }
        }
        ]
      })
    })
  }
}
